<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/9
 * Time: 下午2:48
 */

namespace App\Admin\Controllers;

use App\Busi\Traits\PolicyTrait;
use App\Busi\Traits\UserTrait;
use App\Busi\Traits\VoucherTrait;
use App\Http\Controllers\Controller;
use App\Models\Policy;
use App\Models\User;
use App\Models\Voucher;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\Log;

class PolicyController extends Controller
{
    use ModelForm,VoucherTrait,UserTrait,PolicyTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('订单券套餐列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增订单券套餐列表');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑订单券套餐列表');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(Policy::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $obj = $this;
            $grid->user_id('所属用户')->display(function ($user_id) use($obj){
                return $obj->getUserNameByid($user_id);
            });
            $grid->voucher_id('关联的订单券')->display(function ($voucher_id) use($obj){
                return $obj->getVoucherByID($voucher_id);
            });
            $grid->amount('订单券数量');
            $grid->created_at('创建时间');
            $grid->flag('是否有效')->display(function($flag){
                if( $flag==1 ) return '是';
                return '否';
            });

            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(Policy::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }

            $form->select('user_id', '所属用户')->options(User::pluck("phone","id"));
            $form->select('voucher_id', '关联订单券')->options(Voucher::pluck("name","id"));
            $form->hidden("amount","订单券个数");
            $form->radio('flag', '是否生效')->options([0=>'否', 1=>'是'])->default(1);
            $form->saving(function (Form $form) {
                $voucher_id = $form->voucher_id;
                $amount = $this->getVoucherAmountByID($voucher_id);
                Log::debug("debug:amount is".$amount);
                $user_amount = $this->getUserAmountByVoucherId($voucher_id);
                Log::info("debug:enter saving  user amount is ".$user_amount);
                if(!$user_amount){
                    $form->amount = $amount;
                }


                Log::info("debug:enter saving   ");
            });
        });

    }
}